package gui;


import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ChannelViewToolbar extends JPanel {
	
	private JLabel channelViewName = null;
	private JButton optionsButton = null;

	public ChannelViewToolbar() {
		createUI();
		
	}
	
	 //build the UI for this toolbar
	private void createUI() {
		this.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		
		this.channelViewName = new JLabel("   Test Channel "); //will have to remember to put extra spaces in front of the name; it comes too close to the frame edge, otherwise
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(0, 10, 0, 3);
		this.add(this.channelViewName); //add the "Channel Name" label
		
		JLabel divider1 = new JLabel(" | ");
		constraints.insets = new Insets(0, 3, 0, 3);
		constraints.gridx = 1;
		this.add(divider1, constraints); //add a divider
		
		JLabel filterText = new JLabel(" Filter: ");
		constraints.gridx = 2;
		constraints.weightx = 0.0;
		this.add(filterText, constraints); //add the "Filter" label
		
		constraints.gridx = 3;
		constraints.weightx = 1.0; //I want only the text field to stretch with the window
		constraints.fill = GridBagConstraints.HORIZONTAL;
		this.add(createTextFilter(), constraints); //add the text field filter
		
		JLabel divider2 = new JLabel(" | ");
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridx = 4;
		constraints.weightx = 0.0;
		this.add(divider2, constraints); //add a divider
		
		constraints.gridx = 5;
		constraints.weightx = 0.0;
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(createOptionsButton(), constraints); //add the options button
	}
	
	private JTextField createTextFilter() {
		JTextField textFilter = new JTextField(20);
		textFilter.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//XXX: send filter to the text panel here
			}
		});
		return textFilter;
	}
	
	private JButton createOptionsButton() {
		final JButton optionsButton = new JButton("Options >>");
		optionsButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (optionsButton.getText().equals("Options >>"))
					optionsButton.setText("Options <<");
				else 
					optionsButton.setText("Options >>");
			}
		});
		this.optionsButton = optionsButton; //add to global
		return optionsButton;
	}
	
	public void setChannelViewName(String name) {
		this.channelViewName.setText(name);
	}
	
	public JButton getOptionsButton() {
		return this.optionsButton;
	}
}
